<template>
	<view class="container bg-w">
		<view class="header uni-flex uni-row flex-between">
			<uni-nav-bar title="测点详情">
				<template slot='left'>
					<view class="flex width-100 ">
						<i class='iconfont iconfanhui' @click='goBack()'></i>
					</view>
				</template>
			</uni-nav-bar>
		</view>
		<view class="project-report position-r-top">
			<view>
				<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" style-type="text" active-color="#09AAE9"></uni-segmented-control>
				<uni-list v-for="(item,index) in auditData" :key="index" v-show="current==index">
					<uni-list-item title="工序均分" :rightText="item.score.toString()"></uni-list-item>
					<uni-list-item title="工序进度" showArrow link :to="'/pages/projectAudit/processProgress/ProcessProgress?detailedProId='+item.detailedProId"></uni-list-item>
					<view class="explain"  v-show="item.showOperationGuide == 1 || item.showSafetyRegulations == 1 || item.showMaterials == 1">
						<text class="order">说明</text>
						<uni-list-item v-show="item.showOperationGuide == 1" title="操作指南" link @click="OperationGuideBtn(item.operationGuide)" showArrow></uni-list-item>
						<uni-list-item v-show="item.showSafetyRegulations == 1" title="安全规范" link @click="SafetyRegulationsBtn(item.safetyRegulations)"  showArrow></uni-list-item>
						<uni-list-item v-show="item.showMaterials == 1" title="物料清单" link @click="MaterialsBtn(item.materials)"  showArrow></uni-list-item>
					</view>
					<view class="explain" v-show="equipmentsData.length!=0">
						<text class="order">上传资料</text>
						<view class="">
							<uni-list-item class="" v-for="(item,index) in equipmentsData" :key="index" :title="item.gaugingPoint" :rightText="item.auditStatusDesc" showArrow clickable @click="openConstruction(item)"></uni-list-item>
						</view>
					</view>
				</uni-list>
				<view class="content">
					<view v-show="auditStatus==1">
						<button class="submit-btn" type="submit" @click="auditSubmitHandle('审核驳回')">审核驳回</button>
					</view>
					<view v-show="auditStatus==2">
						<button class="submit-btn" type="submit" @click="auditSubmitHandle('结束工序')">结束工序</button>
						<view class="bottom-text">tip：结束工序后，施工人员无法继续添加上报测点</view>
					</view>
					<view v-show="auditStatus==3">
						<button class="submit-btn" type="submit" @click="auditSubmitHandle('审核评分')">审核评分</button>
						<view class="bottom-text">tip：审核评分后，施工人员无法继续添加上报测点</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { findProcess, equipments, auditSubmit } from '@/common/api/map'
	import ipConfig from '@/utils/ipConfig.js'
	export default {
		data() {
			return {
				projectId: '',
				subProjectId: '',
				assessmentId: '',
				auditData: [], //上报数据
				items: [],
				current: 0,
				equipmentsData: [], //上传资料数据,
				auditStatus: 0 //审核状态
			}
		},
		onLoad(params) {
			this.projectId = params.projectId;
			this.subProjectId = params.subProjectId;
			this.assessmentId = params.assessmentId;
		},
		onReady() {
			this.loadProcess();
		},
		onShow() {
			if (this.auditData.length != 0) {
				findProcess({
					projectId: this.projectId,
					subProjectId: this.subProjectId
				}).then((res) => {
					if(res.code == 20000) this.auditData = res.data;
					equipments(this.auditData[this.current].detailedProId).then((res) => {
						if (res.code == 20000) this.equipmentsData = res.data;
						this.checkAuditStatus(this.auditData[this.current], this.equipmentsData);
					})
				})
			}
		},
		methods: {
			loadProcess() {
				findProcess({
					projectId: this.projectId,
					subProjectId: this.subProjectId
				}).then((res) => {
					if (res.code == 20000) {
						this.auditData = res.data;
						this.auditData.forEach((item) => {
							this.items.push(item.subProcessName);
						})
						equipments(this.auditData[0].detailedProId).then((res) => {
							if (res.code == 20000) this.equipmentsData = res.data;
							this.checkAuditStatus(this.auditData[0], this.equipmentsData);
						})
					}
				})
			},
			
				OperationGuideBtn(operationGuide){
					if(operationGuide){
						// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id='+operationGuide, function(res) {
						//   console.log(res);  
						// });
						uni.downloadFile({
							url: ipConfig.ip + 'admin/sys-file/getFileById?id='+operationGuide,
							success: function (res) {
								let filePath = res.tempFilePath;
								uni.openDocument({
									filePath: filePath,
									success: function (res) {
										console.log('打开文档成功');
									}
								});
							}
						});
					}else{
						uni.showToast({
							title:'暂无数据',
							icon:'none'
						})
					}
				},
				SafetyRegulationsBtn(safetyRegulations){
					if(safetyRegulations){
						// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id='+safetyRegulations, function(res) {
						//   console.log(res);  
						// });
						uni.downloadFile({
							url: ipConfig.ip + 'admin/sys-file/getFileById?id='+safetyRegulations,
							success: function (res) {
								let filePath = res.tempFilePath;
								uni.openDocument({
									filePath: filePath,
									success: function (res) {
										console.log('打开文档成功');
									}
								});
							}
						});
					}else{
						uni.showToast({
							title:'暂无数据',
							icon:'none'
						})
					}
				},
				MaterialsBtn(materials){
					if(materials){
						// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id='+materials, function(res) {
						//   console.log(res);  
						// });
						uni.showLoading({
							title: '文件加载中…'
						})
						uni.downloadFile({
							url: ipConfig.ip + 'admin/sys-file/getFileById?id='+materials,
							success: function (res) {
								uni.hideLoading();
								let filePath = res.tempFilePath;
								uni.openDocument({
									filePath: filePath,
									success: function (res) {
										console.log('打开文档成功');
									}
								});
							}
						});
					}else{
						uni.showToast({
							title:'暂无数据',
							icon:'none'
						})
					}
				},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex;
					equipments(this.auditData[this.current].detailedProId).then((res) => {
						if (res.code == 20000) this.equipmentsData = res.data;
						this.checkAuditStatus(this.auditData[this.current], this.equipmentsData);
					})
				}
			},
			checkAuditStatus(process, equipments) {
				if (process.auditFlag == 1) {	//是否可以审批
					let flag = 0;
					equipments.forEach((item) => {
						if (item.auditStatus == 9) flag++;
					})

					if (flag == 0) {
						process.scoreFlag == 1 ? this.auditStatus = 3 : this.auditStatus = 2;
					} else {
						this.auditStatus = 1;
					}
				} else {
					this.auditStatus = 0;
				}
			},
			auditSubmitHandle(text) {
				uni.showModal({
					title: '提示',
					content: `是否确认${text}`,
					success: (res)=> {
						if (res.confirm) {
							let flag = 0;
							this.equipmentsData.forEach((item) => {
								if(this.auditStatus == 1){
									if (item.auditStatus == 3) flag++;
								}else{
									if (item.auditStatus != 4) flag++;
								}
							})
							if (flag != 0) {
								uni.showToast({
									title: '您有设备未完成审批',
									icon: 'none'
								})
							} else {
								switch (this.auditStatus) {
									case 3:
										let params = {
											processDetailedId: this.auditData[this.current].processDetailedId,
											projectId: this.projectId,
											subProjectId: this.subProjectId,
											reportEquipmentVos: this.equipmentsData
										}
										uni.navigateTo({
											url: '/pages/Application/ConstructionAudit/ConstructionProject/ProjectAudit/ExamineGrade/ExamineGrade?assessmentId='+this.assessmentId+'&params='+encodeURIComponent(JSON.stringify(params))
										})
										break;
									default:
										auditSubmit({
											processDetailedId: this.auditData[this.current].processDetailedId,
											projectId: this.projectId,
											subProjectId: this.subProjectId,
											reportEquipmentVos: this.equipmentsData
										}).then((res) => {
											if (res.code == 20000) {
												this.auditStatus == 1 ?
													uni.showToast({
														title: '驳回成功',
														icon: 'none'
													}) :
													uni.showToast({
														title: '通过成功',
														icon: 'none'
													})
												setTimeout(() => {
													findProcess({
														projectId: this.projectId,
														subProjectId: this.subProjectId
													}).then((res) => {
														if(res.code == 20000) this.auditData = res.data;
														equipments(this.auditData[this.current].detailedProId).then((res) => {
															if (res.code == 20000) this.equipmentsData = res.data;
															this.checkAuditStatus(this.auditData[this.current], this.equipmentsData);
														})
													})
													// uni.navigateBack({
													// 	delta: 1
													// });
												}, 1000)
											}
										})
								}
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			openConstruction(item) {
				if(item.auditStatus){
					uni.navigateTo({
						url: '/pages/projectAudit/constructionData/ConstructionData?standardProId='+item.standardProId+'&standardId='+item.standardId+'&auditStatus='+item.auditStatus
					})
				}else{
					uni.showToast({
						title: '当前设备不可审批',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="less">
	.project-report /deep/ .segmented-control {
		height: 100rpx;
	}

	.submit-btn {
		// position: fixed;
		// bottom: 0;
		// left: 5%;
		width: 90%;
		margin: 20rpx auto;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 28rpx;
		background-color: #09AAE9;
		color: #fff;
		border-radius: 50rpx;
	}
	
	.explain /deep/ .uni-list-item__content-title {
		overflow: hidden;
		display: -webkit-box !important;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
		word-break: break-all;
		line-height: 50rpx;
	}
	
	.bottom-text{
		color: #aaa;
		text-align: center;
	}
</style>
